package com.changgou.web.seckill.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.seckill.feign.SeckillGoodsFeign;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: HuangFu
 * @Date: 2020/2/23 11:17
 * @Description:
 */
@Controller
@RequestMapping("/wseckillgoods")
public class SeckillController {
    @Autowired
    private SeckillGoodsFeign seckillGoodsFeign;

    /**
     * 秒杀页面
     * @return
     */
    @GetMapping("/toindex")
    public String toIndex(){
        return "seckill-index";
    }


    /**
     * 查询秒杀时间段
     * @return
     */
    @GetMapping("/timemenus")
    @ResponseBody
    public Result<List<String>> timeMenus() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Date> dateMenus = DateUtil.getDateMenus();
        List<String> resultStr = new ArrayList<>();
        dateMenus.stream().forEach(date -> {
            resultStr.add(simpleDateFormat.format(date));
        });
        return new Result(true, StatusCode.OK,"查询成功",resultStr);
    }

    /**
     * 查询秒杀商品根据时间
     * @param time
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<List<SeckillGoods>> list(@RequestParam("time") String time){
        return seckillGoodsFeign.list(DateUtil.formatStr(time));
    }

}
